#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Author : Tinfeng

import yaml
import os
import xlrd
from xlrd import open_workbook


class ReadYaml:
    def __init__(self,):
        if os.path.exists('..\\Config\\browser.yml'):
            print(11)
        else:
            raise FileNotFoundError('文件不存在!')
        self._data = None



    #@property
    def read_data(self):
        '''

        加载yaml数据
        '''
        #if not self._data:
        with open('..\\Config\\browser.yml','rb') as fr:
                    data = yaml.full_load(fr)
        return data

    def get_value(self,key,port):
        '''

        :param key:
        :param port:
        获取value
        '''
        data = self.read_data()
        value = data[key][port]
        return value

    def write_data(self,i,device,bp,port):
        '''

        :param i:
        :param device:
        :param bp:
        :param port:
        写入数据
        '''
        data = self.join_data(i,device,bp,port)
        with open('..\\Config\\browser.yml',"a") as fr:
            yaml.dump(data,fr)

    def join_data(self,i,device,bp,port):
        data = {
        "user_info_"+str(i):{
        "deviceName":device,
        "bp":bp,
        "port":port
        }
        }
        return data

    def clear_data(self):
        with open('..\\Config\\browser.yml', "w") as fr:
                fr.truncate()
        fr.close()

    def get_file_lines(self):
        data = self.read_data()
        return len(data)

if __name__ == '__main__':
    y = '..//Config//browser.yml'
    reader = ReadYaml()
    print(reader.get_value("brwserType","fireFox"))
